﻿var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加公告',
                    url: config.webportal_server + 'pages/system/notice/form.html?access_token=' + config.getToken().access_token,
                    width: 1000,
                    height: 650,
                    maxmin: true,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑公告',
                        url: config.webportal_server + 'pages/system/notice/form.html?access_token=' + config.getToken().access_token,
                        width: 1000,
                        height: 650,
                        maxmin: true,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'notices/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 发布
            $('#lr_publish').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认要【发布】消息！', function (res) {
                        if (res) {
                            learun.postForm2(config.base_server() + 'notices/publish?id=' + keyValue + '&access_token=' + config.getToken().access_token, {
                                status: 1
                            }, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 查看
            $('#lr_view').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'View',
                        title: '查看公告',
                        url: config.webportal_server + 'pages/system/notice/view.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 1080,
                        height: 760,
                        btn: ['关闭']
                    });
                }
            });
            // 查看阅读人员
            $('#lr_read').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'View',
                        title: '查看公告',
                        url: config.webportal_server + 'pages/system/notice/read.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 400,
                        height: 450,
                        btn: ['关闭']
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'notices/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '公告标题', name: 'title', index: 'title', width: 600, align: 'left'},
                    {label: '公告类别', name: 'category', index: 'category', width: 100, align: 'center'},
                    {
                        label: "发布时间", name: "publicTime", index: "publicTime", width: 140, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }
                    },
                    {label: '阅读次数', name: 'pv', index: 'pv', width: 80, align: 'center'},
                    {
                        label: "发布状态", name: "status", index: "status", width: 80, align: "center", autowidth: false,
                        formatter: function (cellvalue) {
                            if (cellvalue == 2) {
                                return "<span class=\"label label-success\">已发布</span>";
                            } else {
                                return "<span class=\"label label-danger\">未发布</span>";
                            }
                        }
                    }
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '查看公告',
                            url: config.webportal_server + 'pages/system/notice/view.html?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                            width: 1000,
                            height: 650,
                            maxmin: true
                        });
                    }
                },
                mainId: 'id',
                reloadSelected: true,
                isPage: true
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };
    // 保存数据后回调刷新
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    }
    page.init();
}


